package com.xsq.utils;

import com.google.gson.Gson;
import com.xsq.Constants;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class HttpUtil {

    private volatile static HttpUtil instance;

    /**
     * Double Check 单例模式
     * @return
     */
    public static HttpUtil getInstance() {
        if (instance == null) {
            synchronized (HttpUtil.class) {
                if (instance == null) {
                    instance = new HttpUtil();
                }
            }
        }
        return instance;
    }

    public Callback.Cancelable get(String url, Map<String, String> maps, Callback.CommonCallback callback) {
        if(maps == null) {
            maps = new HashMap<>();
        }
        /*if(needToken) {
            maps.put("accessToken", BaseApplication.getInstance().getDefaultAccount().getAccessToken());
        }*/
        maps.put("orgCode", Constants.orgCode);
        RequestParams params = new RequestParams(url);
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            params.addBodyParameter(entry.getKey(), entry.getValue());
        }
        params.addBodyParameter("sign", Utils.getSign(maps, Constants.SIGN_KEY));
        Callback.Cancelable cancelable = x.http().get(params, callback);
        return cancelable;
    }

    public Callback.Cancelable post(String url, Map<String, String> maps, Callback.CommonCallback callback) {
        if(maps == null) {
            maps = new HashMap<>();
        }
        /*if(needToken) {
            maps.put("accessToken", BaseApplication.getInstance().getDefaultAccount().getAccessToken());
        }*/
        maps.put("orgCode", Constants.orgCode);
        maps.put("sign", Utils.getSign(maps, Constants.SIGN_KEY));
        Gson gson = new Gson();
        String jsonStr = gson.toJson(maps);
        RequestParams params = new RequestParams(url);
        params.setAsJsonContent(true);
        params.setBodyContent(jsonStr);
        /*for (Map.Entry<String, String> entry : maps.entrySet()) {
            params.addBodyParameter(entry.getKey(), entry.getValue());
        }
        params.addBodyParameter("sign", Utils.getSign(maps, Constants.SIGN_KEY));*/
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    public Callback.Cancelable uploadFile(String url, File file, Map<String, String> maps, Callback.ProgressCallback callback) {
        if(maps == null) {
            maps = new HashMap<>();
        }
        /*if(needToken) {
            maps.put("accessToken", BaseApplication.getInstance().getDefaultAccount().getAccessToken());
        }*/
        maps.put("timestamp", String.valueOf(System.currentTimeMillis()));
        RequestParams params = new RequestParams(url);
        for (Map.Entry<String, String> entry : maps.entrySet()) {
            params.addBodyParameter(entry.getKey(), entry.getValue());
        }
        params.addBodyParameter("file", file);
        params.addBodyParameter("sign", Utils.getSign(maps, Constants.SIGN_KEY));
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }
}
